package pages;

import java.io.File;
import java.lang.reflect.Field;
import java.util.HashMap;

import common.ReadPageXML;

//This class reads the Google Search Result Page XML file and creates the variables with the values specified in XML file.
public class GoogleSearchResultsPage {

	public String ResultStats;
	public String OptionsButtonIcon;
	
	public GoogleSearchResultsPage() {
		
		String pageFileName = this.getClass().getSimpleName();
		System.out.println("Calling class name is " + pageFileName);
		
		//form the xml file path
		File directory = new File (".");
		String absPath = null;
		try {
			System.out.println ("Current directory's absolute  path: " 
					+ directory.getAbsolutePath());
			absPath = new String(directory.getAbsolutePath());
		}catch(Exception e) {
			 	System.out.println("Exceptione is ="+e.getMessage());
		}
         
		String temp [] = null;
		temp = absPath.split("\\.");
		String xmlPath = temp[0] + "src\\Pages\\xml\\" + pageFileName + ".xml";
		System.out.println("Xml file path is => " + xmlPath);

		//call the function to parse the xml 
		ReadPageXML xml_parser = new ReadPageXML();
		HashMap<String, String> map = xml_parser.parseXML(xmlPath);

		Field[] fields = GoogleSearchResultsPage.class.getFields();
		System.out.println("Field length = " + fields.length);
		
		for (int k = 0; k < fields.length; k++) {
			if (fields[k].getName().equalsIgnoreCase("ResultStats")) {
				setResultStats(map.get("ResultStats"));	
			}
			if (fields[k].getName().equalsIgnoreCase("OptionsButtonIcon")) {
				setOptionsButtonIcon(map.get("OptionsButtonIcon"));	
			}
		}
		System.out.println("Value of ResultStats is = " + getResultStats());
		System.out.println("Value of OptionsButton is = " + getOptionsButtonIcon());
	}

	public String getResultStats() {
		return ResultStats;
	}

	public void setResultStats(String resultStats) {
		ResultStats = resultStats;
	}

	public String getOptionsButtonIcon() {
		return OptionsButtonIcon;
	}

	public void setOptionsButtonIcon(String optionsButtonIcon) {
		OptionsButtonIcon = optionsButtonIcon;
	}
	
}
